package org.spring.springboot.app.sys.dict.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.spring.springboot.app.base.R;
import org.spring.springboot.app.base.Type;
import org.spring.springboot.app.sys.dict.domain.po.SysDictPO;
import org.spring.springboot.app.sys.dict.domain.vo.SysDictTypeVO;
import org.spring.springboot.app.sys.dict.domain.vo.SysDictVO;
import org.spring.springboot.app.sys.dict.service.ISysDictService;
import org.spring.springboot.component.annotation.AccessTokenVerify;
import org.spring.springboot.util.ApiIndex;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(description="字典操作接口",tags = ApiIndex.SysDictController)
@RequestMapping(value = "/api/sysDict")
@RestController
public class SysDictController {

    @Autowired
    private ISysDictService sysDictService;

    @ApiOperation(value = "根据类型获取列表信息")
    @RequestMapping(value = "/findByType", method = RequestMethod.GET)
    public R<List<SysDictVO>> findByType(
            @ApiParam(value = "类型Code", required = true) @RequestParam(defaultValue = "") String type
    ) {
        R<List<SysDictVO>> result = new R<List<SysDictVO>>();
        List<SysDictPO> _list = sysDictService.findByType(type);
        List<SysDictVO> list = getSysDictVOS(_list);
        result.setData(list);
        return result;
    }

    @ApiOperation(value = "根据类型和值获取单个信息")
    @RequestMapping(value = "/findByTypeValue", method = RequestMethod.GET)
    public R<SysDictVO> findByTypeValue(
            @ApiParam(value = "类型Code", required = true) @RequestParam(defaultValue = "") String type,
            @ApiParam(value = "类型值", required = true) @RequestParam(defaultValue = "") String value
    ) {
        R<SysDictVO> result = new R<SysDictVO>();
        SysDictPO po = sysDictService.findByTypeValue(type, value);
        SysDictVO vo = new SysDictVO();
        BeanUtils.copyProperties(po,vo);
        result.setData(vo);
        return result;
    }

    @ApiOperation(value = "根据类型获取最大排序")
    @RequestMapping(value = "/findMaxSortByType", method = RequestMethod.GET)
    public R<Long> findMaxSortByType(
            @ApiParam(value = "类型Code", required = true) @RequestParam(defaultValue = "") String type
    ) {
        R<Long> result = new R<Long>();
        Long maxSort = 0L;
        List<SysDictPO> list = sysDictService.findPage(0,Integer.MAX_VALUE,type);
        if (list != null) {
            for (SysDictPO po : list) {
                if (maxSort <= po.getSort()) {
                    maxSort = po.getSort();
                }
            }
        }
        result.setData(maxSort+10);
        return result;
    }

    @ApiOperation(value = "获取所有类型列表")
    @RequestMapping(value = "/findAllType", method = RequestMethod.GET)
    public R<List<SysDictTypeVO>> findAllType() {
        R<List<SysDictTypeVO>> result = new R<List<SysDictTypeVO>>();
        List<SysDictTypeVO> list = sysDictService.findAllType();
        result.setData(list);
        return result;
    }

    @ApiOperation(value = "查询字典列表-分页")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public R<PageInfo<List<SysDictVO>>> findPage(
            @ApiParam(value = "名称", required = true) @RequestParam(defaultValue = "") String type,
            @ApiParam(value = "页数", required = true) @RequestParam(defaultValue = "1") int pageNum,
            @ApiParam(value = "每页条数", required = true) @RequestParam(defaultValue = "10") int pageSize
    ) {
        R<PageInfo<List<SysDictVO>>> result = new R<PageInfo<List<SysDictVO>>>();
        List<SysDictPO> _list = sysDictService.findPage(pageNum, pageSize, type);
        PageInfo p =new PageInfo(_list);
        p.setList(getSysDictVOS(p.getList()));
        result.setData(p);
        return result;
    }

    private List<SysDictVO> getSysDictVOS(List<SysDictPO> _list) {
        List<SysDictVO> list = new ArrayList<SysDictVO>();
        if (_list != null && _list.size()>0) {
            for (SysDictPO po : _list) {
                SysDictVO vo = new SysDictVO();
                BeanUtils.copyProperties(po,vo);
                list.add(vo);
            }
        }
        return list;
    }

    @ApiOperation(value = "查询字典列表-所有")
    @RequestMapping(value = "/queryAll", method = RequestMethod.GET)
    public R<PageInfo<List<SysDictVO>>> queryAll() {
        R<PageInfo<List<SysDictVO>>> result = new R<PageInfo<List<SysDictVO>>>();
        List<SysDictPO> _list = sysDictService.findAll();
        PageInfo p =new PageInfo(_list);
        p.setList(getSysDictVOS(p.getList()));
        result.setData(p);
        return result;
    }

    @ApiOperation(value = "根据Id获取单个信息")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public R<SysDictVO> find(
            @ApiParam(value = "字典id", required = true) @PathVariable("id") String id
    ) {
        R<SysDictVO> result = new R<SysDictVO>();
        SysDictPO po = sysDictService.find(id);
        SysDictVO vo = new SysDictVO();
        BeanUtils.copyProperties(po,vo);
        result.setData(vo);
        return result;
    }

    @ApiOperation(value = "保存字典信息")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "", method = RequestMethod.POST)
    public R save(
            @ApiParam(value = "字典对象", required = true) @RequestBody SysDictVO sysDictVO
    ) {
        R result = new R();
        SysDictPO po = new SysDictPO();
        BeanUtils.copyProperties(sysDictVO,po);
        int code = sysDictService.save(po);
        if (code == 0) {
            result.setType(Type.OperError);
            result.setMessage("保存失败");
        }
        return result;
    }


    @ApiOperation(value = "修改字典信息")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public R update(
            @ApiParam(value = "字典对象", required = true) @RequestBody SysDictVO sysDictVO
    ) {
        R result = new R();
        SysDictPO po = new SysDictPO();
        BeanUtils.copyProperties(sysDictVO,po);
        int code = sysDictService.update(po);
        if (code == 0) {
            result.setType(Type.OperError);
            result.setMessage("修改失败");
        }
        return result;
    }

    @ApiOperation(value = "删除字典信息")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public R delete(
            @ApiParam(value = "字典id", required = true) @PathVariable("id") String id
    ) {
        R result = new R();
        int code = sysDictService.delete(id);
        if (code == 0) {
            result.setType(Type.OperError);
            result.setMessage("删除失败");
        }
        return result;
    }

}
